//
//  JLCFBaseTableViewController.m
//  QTZ
//
//  Created by ski on 2017/8/2.
//  Copyright © 2017年 ski. All rights reserved.
//

#import "JLCFBaseTableViewController.h"
#import "JLCFTableViewCell.h"

@interface JLCFBaseTableViewController ()

@end

@implementation JLCFBaseTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addNavigationBar];
    [self addTableView];
    
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIApplicationBackgroundFetchIntervalNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
        // Fallback on earlier versions
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addNavigationBar
{
    if (!self.hideNavigationBar) {
        [super addNavigationBar];
    }
    else
    {
        return;
    }
}

- (void)addTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.hideNavigationBar ? 0 : NAVBAR_HEIGHT, self.view.width, self.view.height - (self.hideNavigationBar ? 0 : NAVBAR_HEIGHT) - (IPHONEX ? 30 : 0)) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];//下拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];//上拉加载
    if (![self.tableView superview]) {
        [self.view addSubview:self.tableView];
    }
    
    UITapGestureRecognizer *singleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)];
    singleTapGesture.numberOfTapsRequired = 1;
    singleTapGesture.cancelsTouchesInView = NO;
    [self.tableView addGestureRecognizer:singleTapGesture];
    
}

- (void)tap:(UITapGestureRecognizer *)tapGes{
    
    [self.tableView endEditing:YES];
    
}

//下拉刷新
- (void)loadData
{
    
}
//上拉加载
- (void)loadMoreData
{
    
}
//开始刷新
- (void)startRefresh
{
    [self.tableView.mj_header beginRefreshing];
}
//结束刷新
- (void)endRefresh
{
    [self.tableView.mj_header endRefreshing];
}
//结束加载
- (void)endLoadMore
{
    [self.tableView.mj_footer endRefreshing];
}
//footer暂无更多数据
- (void)showNoMoreDataNotice:(NSString *)text{
    
    self.tableView.mj_footer.hidden = YES;
    if (!text) {
        text = @"暂无更多数据";
    }
    UILabel *noMoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, FULL_WIDTH, 50)];
    noMoreLabel.text = text;
    noMoreLabel.font = FONT(13);
    noMoreLabel.textColor = QTZ_Gray118;
    noMoreLabel.textAlignment = NSTextAlignmentCenter;
    
    [self.tableView setTableFooterView:noMoreLabel];
}
//隐藏footer
- (void)hideNoMoreDataNotice{
    self.tableView.mj_footer.hidden = NO;
    [self.tableView setTableFooterView:nil];
}
//隐藏上拉加载
- (void)hideMj_foot
{
    self.tableView.mj_footer.hidden = YES;
}
//显示上拉加载
- (void)showMj_foot
{
    self.tableView.mj_footer.hidden = NO;
}
//网络不好提示
- (void)JLCFErrorTipsViewBeginRefresh:(JLCFErrorTipsView *)tipsView
{
    [self hideErrorTips];
    [self loadData];
}


#pragma mark ------UITableViewDataSource-----
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    JLCFTableViewCell *cell = [JLCFTableViewCell dequeueReusableCellForTableView:tableView];
    [cell reloadData];
    return cell;
}

- (void)dealloc
{
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
}

@end
